<template lang="pug">
mt-drawer(v-model="drawer" position="absolute" title="标本轨迹")
  el-timeline
    el-timeline-item(v-for="(item, index) in activities" :key="index" :timestamp="item.timestamp" placement="top" :color="item.color" )
      mt-card(style="border-radius: 5px;border:1px solid #ccc")
        span(style="font-size: 14px;color: rgb(0, 0, 0);") {{item.extrartitle}}
        span(style="font-size: 14px;color: rgb(0, 0, 0);font-weight:600") {{item.extrartitleContent}}
        span(style="margin-left: 6px;") {{item.content}}
        span(style="font-size: 14px;color: rgb(0, 0, 0);font-weight:600") {{item.contentTime}}
        div(style="font-size: 14px;color: #303133;") 
          span(style="font-size: 14px;color: rgb(0, 0, 0);") {{item.extrartimestamp??""}}
          span(style="font-size: 14px;color: rgb(0, 0, 0);font-weight:600") {{item.extrartimestampContent??""}}
          
</template>

<script setup lang="ts">
import { ESpecimenStatus } from "@scmt/api-common";
import { type SpecimenEntity } from "@scmt/api-lis-specimen";
import { useRequest } from "@scmt/common-hooks";
import { useApi } from "@scmt/core-api";
const $api = useApi();
const drawer = defineModel({
  default: false,
});

//添加 Props属性
const props = defineProps({
  selectionRow: {
    type: Object as PropType<SpecimenEntity>,
    default: () => {},
  },
});

onMounted(() => {
  request("listDoctorAsOptions");
  request("listAsOptions");
  request("listequipmentAsOptions");
});

const { request, data, loadingMap } = useRequest({
  listDoctorAsOptions: (val: string) =>
    $api.user.listDoctorAsOptions({ officeId: val }),
  listAsOptions: () => $api.upms.org.listAsOptions(),
  listequipmentAsOptions: () => $api.lis.equipment.listAsOptions(),

  getSpecimenById: (ids: string) => {
    return $api.lis.specimen.get(ids).then((res) => {
      if (res) {
        const {
          billingDepartment,
          billingDoctor,
          barcodeGeneratesId,
          barcodePrintingId,
          collectionId,
          sendInspectionId,
          signee,
          deviceId,
          inspector,
          reviewer,
          barcode,
        } = res;

        const getColorForState = (state: number) => {
          if (
            state === 0 ||
            state === 2 ||
            state === 3 ||
            state === 4 ||
            state === 5 ||
            state === 6 ||
            state === 7 ||
            state === 8 ||
            state === 9
          ) {
            return "#0083fa";
          }
          return "";
        };

        activities.value[0].color = getColorForState(res.state);
        activities.value[0].extrartitle = "开单科室:";
        activities.value[0].extrartitleContent = ` ${
          data.listAsOptions?.find((i) => i.value == billingDepartment)
            ?.label || "--"
        }`;
        activities.value[0].extrartimestamp = "开单医生:";
        activities.value[0].extrartimestampContent = ` ${
          data.listDoctorAsOptions?.find((i) => i.value == billingDoctor)
            ?.label || "--"
        }`;
        activities.value[0].content = "开单时间";
        activities.value[0].contentTime = res.billingTime;

        if (
          res.state >= ESpecimenStatus.Done &&
          res.state != ESpecimenStatus.Rejection
        ) {
          activities.value[1].color = "#0083fa";
          activities.value[1].content = "生成时间:";
          activities.value[1].contentTime = res.barcodeTime;
          activities.value[1].extrartitle = "操作人:";
          activities.value[1].extrartitleContent = ` ${
            data.listDoctorAsOptions?.find((i) => i.value == barcodeGeneratesId)
              ?.label || "--"
          }`;
          activities.value[1].extrartimestamp = "条码号:";
          activities.value[1].extrartimestampContent = res.barcode || "--";

          if (
            res.state >= ESpecimenStatus.PrintedBarCode &&
            res.state != ESpecimenStatus.Rejection
          ) {
            activities.value[2].color = "#0083fa";
            activities.value[2].content = "打印时间:";
            activities.value[2].contentTime = res.barcodePrintingTime;
            activities.value[2].extrartitle = "操作人:";
            activities.value[2].extrartitleContent = ` ${
              data.listDoctorAsOptions?.find(
                (i) => i.value == barcodePrintingId
              )?.label || "--"
            }`;
          }

          if (
            res.state >= ESpecimenStatus.Collection &&
            res.state != ESpecimenStatus.Rejection
          ) {
            activities.value[3].color = "#0083fa";
            activities.value[3].content = "采集时间:";
            activities.value[3].contentTime = res.collectionTime;
            activities.value[3].extrartitle = "操作人:";
            activities.value[3].extrartitleContent = ` ${
              data.listDoctorAsOptions?.find((i) => i.value == collectionId)
                ?.label || "--"
            }`;
          }

          if (
            res.state >= ESpecimenStatus.Submission &&
            res.state != ESpecimenStatus.Rejection
          ) {
            activities.value[4].color = "#0083fa";
            activities.value[4].content = "送检时间:";
            activities.value[4].contentTime = res.sendInspectionTime;
            activities.value[4].extrartitle = "操作人:";
            activities.value[4].extrartitleContent = ` ${
              data.listDoctorAsOptions?.find((i) => i.value == sendInspectionId)
                ?.label || "--"
            }`;
            activities.value[4].extrartimestamp = "批次号:";

            activities.value[4].extrartimestampContent = res.batchNumber;
          }

          if (
            res.state >= ESpecimenStatus.Receipt &&
            res.state != ESpecimenStatus.Rejection
          ) {
            activities.value[5].color = "#0083fa";
            activities.value[5].content = "签收时间:";
            activities.value[5].contentTime = res.signingTime;
            activities.value[5].extrartitle = "操作人:";
            activities.value[5].extrartitleContent = ` ${
              data.listDoctorAsOptions?.find((i) => i.value == signee)?.label ||
              "--"
            }`;
          }

          if (res.state >= ESpecimenStatus.Verification) {
            activities.value[6].color = "#0083fa";
            activities.value[6].content = "检验时间:";
            activities.value[6].contentTime = res.inspectionTime;
            activities.value[6].extrartitle = "仪器:";
            activities.value[6].extrartitleContent = ` ${
              data.listequipmentAsOptions?.find((i) => i.value == deviceId)
                ?.label || "--"
            }`;
            activities.value[6].extrartimestamp = "检验人:";
            activities.value[6].extrartimestampContent = ` ${
              data.listDoctorAsOptions?.find((i) => i.value == inspector)
                ?.label || "--"
            }`;
          }

          if (res.state >= ESpecimenStatus.Examine) {
            activities.value[7].color = "#0083fa";
            activities.value[7].content = "审核时间:";
            activities.value[7].contentTime = res.reviewTime;
            activities.value[7].extrartitle = "操作人";
            activities.value[7].extrartitleContent = ` ${
              data.listDoctorAsOptions?.find((i) => i.value == reviewer)
                ?.label || "--"
            }`;
          }
        }
      }
    });
  },
});

const getLabelByValue = (val: string) => {
  return data.listDoctorAsOptions?.find((i) => i.value === val)?.label || "";
};

const activities = ref([
  {
    content: "开单时间:--",
    contentTime: "--",
    timestamp: "医生开单",
    extrartitle: "",
    extrartitleContent: "",
    extrartimestamp: "开单医生:--",
    extrartimestampContent: "",
    color: "",
  },
  {
    content: "生成时间:",
    contentTime: "--",
    timestamp: "条码生成",
    extrartitle: "操作人:",
    extrartitleContent: "--",
    extrartimestamp: "条码号:",
    extrartimestampContent: "--",
    color: "",
  },
  {
    content: "打印时间:--",
    contentTime: "--",
    timestamp: "条码打印",
    extrartitle: "操作人:--",
    extrartitleContent: "--",
    extrartimestamp: "批次号:",
    extrartimestampContent: "--",
    color: "",
  },
  {
    content: "采集时间:--",
    contentTime: "--",
    timestamp: "标本采集",
    extrartitle: "采集人:--",
    extrartitleContent: "--",
    extrartimestampContent: "--",
    extrartimestamp: "",
    color: "",
  },
  {
    content: "送检时间:",
    contentTime: "--",
    timestamp: "标本送检",
    extrartitle: "送检人:",
    extrartitleContent: "--",
    extrartimestamp: "",
    extrartimestampContent: "",
    color: "",
  },
  {
    content: "签收时间:--",
    contentTime: "--",
    timestamp: "标本签收",
    extrartitle: "签收人:",
    extrartitleContent: "--",
    extrartimestamp: "",
    extrartimestampContent: "",
    color: "",
  },
  {
    content: "检验时间:",
    contentTime: "--",
    timestamp: "标本检验",
    extrartitle: "仪器:",
    extrartitleContent: "--",
    extrartimestamp: "检验人:",
    extrartimestampContent: "--",
    color: "",
  },
  {
    content: "审核时间:",
    contentTime: "--",
    timestamp: "报告审核",
    extrartitle: "审核人:",
    extrartitleContent: "--",
    extrartimestamp: "",
    extrartimestampContent: "",
    color: "",
  },
]);
watch(
  drawer,
  (visible: boolean) => {
    if (visible) {
      activities.value = [
        {
          content: "开单时间:--",
          contentTime: "--",
          timestamp: "医生开单",
          extrartitle: "",
          extrartitleContent: "",
          extrartimestamp: "开单医生:--",
          extrartimestampContent: "",
          color: "",
        },
        {
          content: "生成时间:",
          contentTime: "--",
          timestamp: "条码生成",
          extrartitle: "操作人:",
          extrartitleContent: "--",
          extrartimestamp: "条码号:",
          extrartimestampContent: "--",
          color: "",
        },
        {
          content: "打印时间:--",
          contentTime: "--",
          timestamp: "条码打印",
          extrartitle: "操作人:--",
          extrartitleContent: "--",
          extrartimestamp: "",
          extrartimestampContent: "",
          color: "",
        },
        {
          content: "采集时间:--",
          contentTime: "--",
          timestamp: "标本采集",
          extrartitle: "采集人:--",
          extrartitleContent: "--",
          extrartimestampContent: "",
          extrartimestamp: "",
          color: "",
        },
        {
          content: "送检时间:",
          contentTime: "--",
          timestamp: "标本送检",
          extrartitle: "送检人:",
          extrartitleContent: "--",
          extrartimestamp: "批次号",
          extrartimestampContent: "--",
          color: "",
        },
        {
          content: "签收时间:--",
          contentTime: "--",
          timestamp: "标本签收",
          extrartitle: "签收人:",
          extrartitleContent: "--",
          extrartimestamp: "",
          extrartimestampContent: "",
          color: "",
        },
        {
          content: "检验时间:",
          contentTime: "--",
          timestamp: "标本检验",
          extrartitle: "仪器:",
          extrartitleContent: "--",
          extrartimestamp: "检验人:",
          extrartimestampContent: "",
          color: "",
        },
        {
          content: "审核时间:",
          contentTime: "--",
          timestamp: "报告审核",
          extrartitle: "审核人:",
          extrartitleContent: "--",
          extrartimestamp: "",
          extrartimestampContent: "",
          color: "",
        },
      ];

      if (props.selectionRow.id) {
        request("getSpecimenById", props.selectionRow.id);
      }
    }
  },
  {
    immediate: true,
  }
);
</script>

<style lang="less" scoped></style>
